<%@page import="java.sql.ResultSet" %>
<%@page import="java.sql.Connection"%>

<%
            String data = "";
            String sql = "";
            int upd = 0;
            int errorcode = 0;
            model.ConnectionDB connection = new model.ConnectionDB();
            ResultSet res = null;

             if (request.getParameter("Action").equals("Seek")) {
                sql = "SELECT * FROM SeekAllDrivers()";

                 try {
                    connection.connect();

                    res = connection.consult(sql);
                    connection.getCon().close();

                    while (res.next()) {

                        if (!data.equals("")) {
                            data += ",";
                        }
                        data += '{';
                        data += "'nameDriver' :'" + res.getString("driver_name") + "', ";
                        data += "'typeDriver' :'" + res.getString("driver_type") + "', ";
                        data += "'idDriver': " + res.getInt("driver_id") + ", ";
                        data += '}';
                    }
                } catch (Exception e) {
                    out.print(e.getMessage());
                    errorcode = -1;
                  }
           }else if(request.getParameter("Action").equals("Update")){
                    sql = "SELECT UpdateDriver("+request.getParameter("ID")+",'"+request.getParameter("Name")+"','"+request.getParameter("Type")+"')";
                      try {
                            connection.connect();
                            res = connection.consult(sql);
                            connection.getCon().close();
                            if(res.next())
                                data="{'ans' : " + res.getInt(1) + "}";//update
                    } catch (Exception e) {
                            out.print(e.getMessage());
                             errorcode = -8;
                  }
           }else if(request.getParameter("Action").equals("Add")){
                    sql = "SELECT * FROM addDriver("+request.getParameter("ID")+",'"+request.getParameter("Name")+"','"+request.getParameter("Type")+"')";
                    try {
                            connection.connect();
                            res=connection.consult(sql);
                            connection.getCon().close();
                            if(res.next())
                                data="{'ans' : " + res.getInt(1) + "}";//Add
                   /* } catch (Exception e) {
                            if(res.next())
                                data="{'ans' : " + res.getInt(1) + "}";//Add  */
                    } catch (Exception e) {
                            errorcode = -7;
                            connection.getCon().close();
                    }
                  
                  }else if(request.getParameter("Action").equals("Delete")){

                  sql = "SELECT DeleteDriver("+request.getParameter("ID")+")";
                    try {
                            connection.connect();
                            connection.consult(sql);
                            connection.getCon().close();

                    } catch (Exception e) {
                            out.print(e.getMessage());
                                errorcode = -1;
                  }
            }


            String json = "{'errorcode' : " + errorcode + ", 'data' : [" + data + "]}";

            out.print(json);
%>